package wsem.application;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Scanner;

import org.xml.sax.SAXException;

import sparqlclient.SparqlClient;
import sparqlclient.SparqlClientExample;
import wsem.model.Document;
import wsem.services.IServicesWsem;
import wsem.services.ServiceWSEMImpl;
import wsem.utils.ConfigUtils;

public class ApplicationMain {

	private IServicesWsem services = new ServiceWSEMImpl();

	public IServicesWsem getServices() {
		return services;
	}

	public void setServices(IServicesWsem services) {
		this.services = services;
	}

	public ApplicationMain(HashMap<String, String> pDataCollectionMap,
			List<Document> collectionDoc, String choice) throws SQLException, SAXException,
			IOException {
		if(Integer.valueOf(choice) == 1){
			System.out.println("--INDEXATION--");
			getServices().indexAllData(pDataCollectionMap, collectionDoc);
		}
		

	}
	
	public ApplicationMain(String choice, String expression) throws SQLException{
		
		if(Integer.valueOf(choice) == 2){
			getServices().searchExpressionPertinent(expression);
		}
		if(expression == null){
			//choix de la precision dans config.properties
			getServices().execEvaluator(Integer.valueOf(ConfigUtils.PRECISION));
		}
	}

	public static void main(String[] args) {

		
		
		System.out.println("#######################################################");
		System.out.println("################# +WSEM Application+ ##################");
		System.out.println("#######################################################");
		
		 
		
		Scanner sc = new Scanner(System.in);
		String choice, queryType;
		int choiceInt, queryTypeInt;

		do {
			System.out.println("Que voulez-vous faire ?");
			System.out.println("    1 : Indexer");
			System.out.println("    2 : Rechercher");
			System.out.println("    3 : Evaluer");
			System.out.println("    0 : Quitter");

			choice = sc.nextLine();
			choiceInt = Integer.valueOf(choice);

			switch (choiceInt) {
			case 1: // realiser l'indexation

				File file = new File(ConfigUtils.PATH_XML_DOCS);

				File[] files = file.listFiles();

				List<Document> collectionDoc = new ArrayList<Document>();

				LinkedHashMap<String, String> dataCollectionMap = new LinkedHashMap<String, String>();

				if (files != null) {
					int compteurDoc = 0;
					for (int i = 0; i < files.length; i++) {
						if (files[i].getName().endsWith("xml")) {
							++compteurDoc;
							

							Document docCurrent = new Document(
									files[i].getName());
							docCurrent.setIdDoc(compteurDoc);
							collectionDoc.add(docCurrent);

							dataCollectionMap.put(files[i].getAbsolutePath(),
									files[i].getName());

						}

					}

				}

				try {
					ApplicationMain execMain = new ApplicationMain(
							dataCollectionMap, collectionDoc,choice);
				} catch (SQLException e) {

					e.printStackTrace();
				} catch (SAXException e) {

					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;

			case 2:
				
				System.out.println("#################### Recherche ################################");
				String motcle="";
				
				
				System.out.println("Saisir un mot cle :");
				motcle = sc.nextLine();
				
				try {
					ApplicationMain execMain = new ApplicationMain(choice,motcle);
				} catch (SQLException e) {
					
					e.printStackTrace();
				}
				
				
				
				break;
			case 3:
				
				
				
				System.out.println("#################### EVALUATION DU SYSTEME ################################");

				
				try {
					ApplicationMain execMain = new ApplicationMain(choice,null);
				} catch (SQLException e) {
					
					e.printStackTrace();
				}
				
				
				
				break;
			case 0: // Quitter
				System.out.println("Exit!");
				System.exit(0);
				break;

			default:
				System.out.println("Choix non disponible...\n");
				break;
			}
		} while (!choice.equals("0"));

	}

}
